﻿/*------------------------------------------------------------------
-- COPYRIGHT (C) 2010-2012  Atom
-- ALL RIGHTS RESERVED.
-- 兆尹科技
-- CREATE DATE: 2010/07/12
-- CREATE MAN:	姜新军
-- 验证实体集合，封装了对实体的验证
-- MODIFY HISTORY:
-- MODIFY DATE:
-- MODIFY MAN:	
-- MODIFY DESC:
-- MODIFY DATE:
-- MODIFY MAN:	
-- MODIFY DESC:
---------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Text;

namespace Atom.Entity
{
    /// <summary>
    /// 验证实体集合
    /// </summary>
    public class SimpleInvalidEntityInfo
    {
        /// <summary>
        /// 实体在集合的序号
        /// </summary>
        public int IndexInList { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public List<string> ErrorMessages { get; set; }
    }

    /// <summary>
    /// 验证实体集合
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class ValidatorListEntity<TEntity>
    {
        #region InvalidEntityInfo 实体错误信息
        /// <summary>
        /// 实体错误信息
        /// </summary>
        public class InvalidEntityInfo : SimpleInvalidEntityInfo
        {
            /// <summary>
            /// 实体
            /// </summary>
            public TEntity Entity { get; set; }
        }
        #endregion

        #region 属性
        private IList<TEntity> list;
        private Dictionary<Predicate<TEntity>, string> predicates;
        private List<InvalidEntityInfo> invalidInfos;

        /// <summary>
        /// 错误信息
        /// </summary>
        public List<InvalidEntityInfo> InvalidInfos
        {
            get
            {
                if (invalidInfos == null)
                {
                    invalidInfos = new List<ValidatorListEntity<TEntity>.InvalidEntityInfo>();
                }
                return invalidInfos;
            }
        }

        /// <summary>
        /// 建议错误信息
        /// </summary>
        /// <remarks>InvalidInfos转换的建议信息</remarks>
        public List<SimpleInvalidEntityInfo> SimpleInvalidInfos
        {
            get
            {
                if (InvalidInfos != null)
                {
                    return InvalidInfos.ConvertAll<SimpleInvalidEntityInfo>(_ele => { return _ele; });
                }
                return null;
            }
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="list">要验证的初始化列表</param>
        public ValidatorListEntity(IList<TEntity> list)
        {
            this.list = list;
            predicates = new Dictionary<Predicate<TEntity>, string>();
        }

        /// <summary>
        /// 设置验证
        /// </summary>
        /// <param name="predicate">错误成立条件</param>
        /// <param name="errorMsg">如果错误成立，显示的错误信息</param>
        /// <returns></returns>
        public ValidatorListEntity<TEntity> SetValidate(Predicate<TEntity> predicate, string errorMsg)
        {
            predicates.Add(predicate, errorMsg);
            return this;
        }

        /// <summary>
        /// 验证集合
        /// </summary>
        /// <returns>是否成功，如果不成功，访问InvalidInfos获取错误数据</returns>
        public bool Valid()
        {
            new List<TEntity>(this.list).ForEach(_entity =>
            {
                ValidatorEntity = new ValidatorEntity<TEntity>(_entity);
                foreach (var predicate in predicates)
                {
                    ValidatorEntity.Validate(predicate.Key, predicate.Value);
                }
                if (ValidatorEntity.ErrorMessages != null && ValidatorEntity.ErrorMessages.Count > 0)
                {
                    //InvalidInfos.Add(new ValidatorListEntity<TEntity>.InvalidEntityInfo()
                    //{
                    //    IndexInList = this.list.IndexOf(_entity),
                    //    Entity = _entity,
                    //    ErrorMessages = validator.ErrorMessages
                    //});
                    SetErrorMessages(_entity, ValidatorEntity);
                }
            });

            return (InvalidInfos.Count == 0);
        }

        public ValidatorEntity<TEntity> ValidatorEntity { get; set; }

        public void SetErrorMessages(TEntity _entity, ValidatorEntity<TEntity> validator)
        {
            InvalidInfos.Add(new ValidatorListEntity<TEntity>.InvalidEntityInfo()
            {
                IndexInList = this.list.IndexOf(_entity),
                Entity = _entity,
                ErrorMessages = validator.ErrorMessages
            });
        }
        #endregion
    }
}
